#include "../../framework/inc/xDxApp.h"
#include "../inc/xWindow.h"
namespace Sky
{
TResult xWindow::onCreate(xDxApp* app )
{
	
	const TChar* name = app->getAppName()->toCharString();
	RECT rc;
	SetRect(&rc,0,0,m_Width,m_Height);
	::AdjustWindowRectEx(&rc,m_Style,vFalse,0);
	m_Res = CreateWindow(name,m_Title->toCharString(),m_Style,m_StartX, m_StartY,rc.right - rc.left ,rc.bottom - rc.top, vNull, vNull,(HINSTANCE)GetModuleHandle(vNull) ,vNull);
	if(m_Res == vNull)
	{
		TUint dwError=GetLastError();
		return myWin_Trace_hr0(E,HRESULT_FROM_WIN32(dwError),_XT("CreateWindow() failed"));
	} // if(m_Res == vNull)
	m_hWnd = m_Res;
	return XE_OK;
}

TResult xWindow::onDelete(xDxApp* app )
{
	return XE_OK;
}

TBool xWindow::attach( HWND hwnd )
{
	if(m_hWnd != hwnd)
	{
		if(m_Res != vNull)
		{
			::ShowWindow(m_Res,SW_HIDE);
		}
	}
	m_hWnd = hwnd;
	RECT rect;
	::GetClientRect(hwnd,&rect);
	m_StartX = rect.left;
	m_StartY = rect.right;
	m_Width = rect.right - rect.left;
	m_Height = rect.bottom - rect.top;
	return vTrue;
}

TVoid xWindow::detach()
{
	m_hWnd = NULL;
}

TVoid xWindow::show( TBool bShow )
{
	::ShowWindow(getHWnd(),bShow ? SW_SHOW : SW_HIDE);
}

TVoid xWindow::close()
{
	if(getHWnd() != vNull)	::SendMessage(getHWnd(),WM_CLOSE,0,0);
}

HWND xWindow::getHWnd()
{
	return m_hWnd;
}

xWindow::xWindow():
m_hWnd(vNull)
{

}

xWindow& xWindow::setTitle( Sky::StringPtr& title )
{
	m_Title = title;return *this;
}

}

